<template>
  <!--销售合同详情-->
  <div class="contract-sale-detail">
    <page-layout :title = "title">
      <detail-list slot="headerContent" size="small" :col="2" class="detail-layout">
        <detail-list-item term="创建人">{{item.operator}}</detail-list-item>
        <detail-list-item term="工程名">{{item.enginerring}}</detail-list-item>
        <detail-list-item term="创建时间">{{dateTimeFormat(item.time)}}</detail-list-item>
        <detail-list-item term="已收款">{{item.received}}</detail-list-item>
        <detail-list-item term="应收款">{{item.accountReceivable}}</detail-list-item>
        <detail-list-item term="应收时间">{{item.accountReceivableTime}}</detail-list-item>
      </detail-list>
      <a-card title="销售合同明细">
        <a @click="toFlush()" slot="extra" href="#">刷新</a>
        <div class="btns" style="padding: 0 0 8px 0">
          <el-button :disabled="selectedRowKeysItems.length > 0?false:true" round type="danger" size="small" icon="el-icon-delete" @click="batchDeletedItems">删除</el-button>
          <el-button :disabled="selectedRowKeysItems.length > 0?false:true" round type="warning" size="small" icon="el-icon-edit-outline" @click="updateItem">修改</el-button>
          <el-button round type="primary" icon="el-icon-circle-plus" size="small" @click="handleAdd">新增</el-button>
          <el-button round type="info" icon="el-icon-printer" size="small" :loading="downloadLoading" @click="download">导出</el-button>
          <el-button round type="primary" icon="el-icon-setting" size="small" @click="templateSelected">设置解析模板</el-button>
          <el-button :disabled="selectedRowKeysItems.length > 0?false:true" round type="warning" icon="el-icon-s-promotion" size="small" @click="sendPurchase">发往采购</el-button>
          <!--导入excel-->
          <el-button round @click="clickFileInput" type="success" size="small" icon="el-icon-upload">excel导入</el-button>
          <input type="file" ref="upload" accept=".xls,.xlsx" @change="readExcel" class="outputlist_upload">
          从Excel读取到<span style="color: #42b983">{{outputs.length}}</span>条数据
          <!--end-->
          <el-button round type="primary" icon="el-icon-plus" size="small" @click="toSearchStock" style="float: right">库存查询</el-button>
        </div>
        <a-table
          :loading = "loading"
          :data-source = "itemData"
          size = "small"
          :rowKey="record => record.id"
          :customRow="rowClick"
          :pagination="pagination"
          :scroll="itemData.length>0?{x:1300}:{}"
          :row-selection="{ selectedRowKeys: selectedRowKeysItems, onChange: itemsSelectionChange, selections: [{
            key: 'all-data',
            text: '全选',
            onSelect: () => {
              this.selectedRowKeysItems =  itemData.map(i=>{return i.id});
            },
          }]}">
          <a-table-column :width="60" title="序号" key="sort" data-index="sort" align="center"/>
          <a-table-column :width="100" key="supplyStatus" title="供货状态" data-index="supplyStatus" ellipsis="true" align="center">
            <template slot-scope="text, record">
              <a-tag color="#EE2C2C" v-if="record.supplyStatus === 0">
                未供货
              </a-tag>
              <a-tag color="#7B7B7B" v-else-if="record.supplyStatus === null">
                无状态
              </a-tag>
              <a-tag color="#1E90FF" v-else-if="record.supplyStatus === 1">
                已供货
              </a-tag>
            </template>
          </a-table-column>
          <a-table-column :width="100" key="stockStatus" title="备货状态" data-index="stockStatus" ellipsis="true" align="center">
            <template slot-scope="text, record">
              <a-tag color="#EE2C2C" v-if="record.stockStatus === 0">
                未备货
              </a-tag>
              <a-tag color="#7B7B7B" v-else-if="record.stockStatus === null">
                无状态
              </a-tag>
              <a-tag color="#1E90FF" v-else-if="record.stockStatus === 1">
                已备货
              </a-tag>
              <a-tag color="#EEE685" v-else-if="record.stockStatus === 2">
                已采购
              </a-tag>
              <a-tag color="#B4EEB4" v-else-if="record.stockStatus === 3">
                已询价
              </a-tag>
              <a-tag color="#9F79EE" v-else-if="record.stockStatus === 4">
                已到货
              </a-tag>
            </template>
          </a-table-column>
          <a-table-column :width="100" key="item" title="设备名" data-index="item" ellipsis="true" align="center"/>
          <a-table-column :width="100" key="number" title="数量" data-index="number" ellipsis="true" align="center"/>
          <a-table-column :width="100" key="code" title="需方物资代码" data-index="code" ellipsis="true" align="center"/>
          <a-table-column :width="100" key="model" title="规格型号" data-index="model" ellipsis="true" align="center"/>
          <a-table-column :width="100" key="unit" title="单位" data-index="unit" ellipsis="true" align="center"/>
          <a-table-column :width="100" key="deliveryTime" title="交货时间" data-index="deliveryTime" ellipsis="true" align="center"/>
          <a-table-column :width="100" key="placeOfUse" title="使用地点" data-index="placeOfUse" ellipsis="true" align="center"/>
          <a-table-column :width="100" key="engineeringCode" title="工程代码" data-index="engineeringCode" ellipsis="true" align="center"/>
          <a-table-column :width="100" key="contacts" title="联系人" data-index="contacts" ellipsis="true" align="center"/>
          <a-table-column :width="100" key="salePrice" title="供货单价" data-index="salePrice" ellipsis="true" align="center"/>
          <a-table-column :width="100" title="供货总额" align="center">
            <template slot-scope="text, record">
              {{Number(record.salePrice*record.number).toFixed(2)}}
            </template>
          </a-table-column>
          <!--   <a-table-column :width="100" key="time" title="创建时间">
               <template slot-scope="text, record">
                 {{dateTimeFormat(record.time)}}
               </template>
             </a-table-column>-->
        </a-table>
      </a-card>
    </page-layout>

    <!--模态框-->
    <el-dialog v-el-drag-dialog :visible.sync="dialog.visible" title="选择添加项" style="width: 120%">
      <el-form v-model="dialog.form" status-icon>
        <a-row style="margin: 0 0 1em 0">
          <a-col :span="10">
            <el-select v-model="dialog.form.proDetailIds" filterable clearable placeholder="请选择项目" value-key="name">
              <el-option v-for="item in projects" :key="item.id" :label="item.projectName" :value="item.id" />
            </el-select>
          </a-col>
          <a-col :span="8">
            <el-input v-model="dialog.form.name" type="text" placeholder="设备名"></el-input>
          </a-col>
          <a-col :span="6">
            <el-button style="margin-left: 5em" type="primary" icon="el-icon-search" @click="toSearchItems">查询</el-button>
          </a-col>
        </a-row>
        <a-card>
          <a-table
            :loading = "dialog.loading"
            :data-source = "dialogData"
            size = "small"
            :rowKey="record => record.id"
            :pagination="dialog.pagination"
            :scroll="dialogData.length>0?{x:1300}:{}"
            :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: indexHandleSelectionChange, selections: [{
              key: 'all-data',
              text: '全选',
              onSelect: () => {
                this.selectedRowKeys =  dialogData.map(item=>{return item.id});
              },
            }]}">
            <a-table-column :width="30" title="序号" align="center">
              <template slot-scope="text, record, index">
                {{index+1}}
              </template>
            </a-table-column>>
            <a-table-column :width="80" align="center" title="状态" key="saleContractId" data-index="saleContractId">
              <template slot-scope="text">
                <el-tag :type="text? 'success':'info'">{{ text?'有合同':'无合同' }}</el-tag>
              </template>
            </a-table-column>
            <a-table-column :width="100" key="item" title="设备名" data-index="item" ellipsis="true" align="center"/>
            <a-table-column :width="100" key="number" title="数量" data-index="number" ellipsis="true" align="center"/>
            <a-table-column :width="100" key="purchaseSupply.supplier" title="供货商" data-index="purchaseSupply.supplier" ellipsis="true" align="center"/>
            <a-table-column :width="100" key="purchaseSupply.model" title="供货型号" data-index="purchaseSupply.model" ellipsis="true" align="center"/>
            <a-table-column :width="100" key="purchaseSupply.brand" title="供货品牌" data-index="purchaseSupply.brand" ellipsis="true" align="center"/>
            <a-table-column :width="100" key="purchaseSupply.params" title="供货参数" data-index="purchaseSupply.params" ellipsis="true" align="center"/>
            <a-table-column :width="100" key="price" title="销售价" data-index="price" ellipsis="true" align="center"/>
            <a-table-column :width="100" title="销售总价" align="center">
              <template slot-scope="text, record">
                {{record.purchaseSupply.price*record.number}}
              </template>
            </a-table-column>
          </a-table>
        </a-card>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialog.visible = false">取消</el-button>
        <el-button type="primary" :loading="dialog.submitLoading" size="small" @click="batchSelected">选取</el-button>
      </div>
    </el-dialog>

    <!--解析模板模态框-->
    <el-dialog v-el-drag-dialog :visible.sync="templateDialog.visible" title="选择解析模板" style="width: 120%">
      <el-form v-model="templateDialog.form" status-icon>
        <el-select v-model="templateDialog.form.templateId" filterable clearable placeholder="请选择模板" value-key="name">
          <el-option v-for="item in templateDialog.dataSource" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="templateDialog.visible = false">取消</el-button>
        <el-button type="primary" :loading="templateDialog.submitLoading" size="small" @click="setAnalysisTemplate">选取</el-button>
      </div>
    </el-dialog>

    <!--修改模态框-->
    <el-dialog v-el-drag-dialog :visible.sync="updateDialog.visible" title="修改" style="width: 120%">
      <el-form v-model="updateDialog.form" status-icon>
        <el-form-item label="设备名" label-width="80px" prop="contractNo">
          <el-input v-model="updateDialog.form.item" autocomplete="off" />
        </el-form-item>
        <el-form-item label="数量" label-width="80px" prop="contractNo">
          <el-input v-model="updateDialog.form.number" autocomplete="off" />
        </el-form-item>
        <el-form-item label="需方物资代码" label-width="80px" prop="contractNo">
          <el-input v-model="updateDialog.form.code" autocomplete="off" />
        </el-form-item>
        <el-form-item label="规格型号" label-width="80px" prop="contractNo">
          <el-input v-model="updateDialog.form.model" autocomplete="off" />
        </el-form-item>
        <el-form-item label="单位" label-width="80px" prop="contractNo">
          <el-input v-model="updateDialog.form.unit" autocomplete="off" />
        </el-form-item>
        <el-form-item label="交货时间" label-width="80px" prop="contractNo">
          <el-input v-model="updateDialog.form.deliveryTime" autocomplete="off" />
        </el-form-item>
        <el-form-item label="使用地点" label-width="80px" prop="contractNo">
          <el-input v-model="updateDialog.form.placeOfUse" autocomplete="off" />
        </el-form-item>
        <el-form-item label="工程代码" label-width="80px" prop="contractNo">
          <el-input v-model="updateDialog.form.engineeringCode" autocomplete="off" />
        </el-form-item>
        <el-form-item label="联系人" label-width="80px" prop="contractNo">
          <el-input v-model="updateDialog.form.contacts" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="updateDialog.visible = false">取 消</el-button>
        <el-button type="primary" size="small" @click="updateCommit('form')">确 定</el-button>
      </div>
    </el-dialog>

    <!--库存模态框-->
    <el-dialog></el-dialog>

    <!--项目选择模态框-->
    <el-dialog v-el-drag-dialog :visible.sync="project.visible" title="发往采购项目" style="width: 80%">
      <el-select v-model="project.id" style="margin-right: 6px" filterable clearable placeholder="请选择项目" value-key="name">
        <el-option v-for="item in projects" :key="item.id" :label="item.projectName" :value="item.id" />
      </el-select>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="project.visible = false">取 消</el-button>
        <el-button type="primary" size="small" @click="changeProject()">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import { getAction, postActionByJson, postActionByQueryString } from '@/api/manage'
  import "@/styles/auto-style.css"
  import { dateTimeFormat, dateFormat } from '@/utils/format'
  import elDragDialog from '@/directive/el-drag-dialog'
  import PageLayout from '@/components/Page/PageLayout'
  import DetailList from '@/components/Page/DetailList'
  import CommonUpload from '@/components/Upload/CommonUpload'
  import XLSX from 'xlsx'
  import { sortBykey } from '@/utils/sort'

  const DetailListItem = DetailList.Item

  export default {
    directives: { elDragDialog },
    components: {
      PageLayout,
      DetailList,
      DetailListItem,
      CommonUpload
    },

    data(){

      return{
        
        project:{
          id: null,
          visible: false
        },
        //导出按钮
        downloadLoading: false,
        stockDialog:{
          form: {},
          loading: false,
          visible: false,
          submitLoading: false,
          dataSource: []
        },
        updateDialog:{
          form: {},
          loading: false,
          visible: false,
          submitLoading: false,
        },
        templateDialog: {
          form: {},
          loading: false,
          visible: false,
          submitLoading: false,
          dataSource: []
        },

        excelRows: null,
        outputs: [],
        itemIds: [],

        selectedRowKeysItems: [],
        selectedRowKeys: [],
        dialogData: [],
        projects: [],
        searchForm: {},
        fileList: [],
        dialog: {
          loading: false,
          visible: false,
          form: {},
          submitLoading: false,
          pagination: {
            showQuickJumper: true,
            showSizeChanger: true,
            pageSizeOptions: ['10', '20', '50'],
            current: 1,
            pageSize: 10,
            total: 0,
            onChange: (item) => {
              this.dialog.pagination.current = item
              this.toSearchItems()
            },
            onShowSizeChange: (current, pageSize) => {
              this.dialog.pagination.pageSize = pageSize
              this.toSearchItems()
            }, // 改变每页数量时更新显示
          }
        },
        pagination: {
          showQuickJumper: true,
          showSizeChanger: true,
          pageSizeOptions: ['10', '20', '50'],
          current: 1,
          pageSize: 10,
          total: 0,
          onChange: (item) => {
            this.pagination.current = item
            this.toSearch()
          },
          onShowSizeChange: (current, pageSize) => {
            this.pagination.pageSize = pageSize
            this.toSearch()
          }, // 改变每页数量时更新显示
        },
        loading: true,
        title: '',
        item: {},
        itemData: [],
        saleId: '',
        proId: '',
        url: {
          findSaleContractDetail : '/contract/sale/findSaleContractDetail',
          findSaleItemBySaleId : '/contract/sale/findSaleItemBySaleId',
          addItems : '/contract/sale/addItems',
          findAllProject: '/purchase/project/findAllLike',
          findItemsAndSupplyByProjectId: '/purchase/generatePurchaseContract/findItemsAndSupplyByProjectId',
          batchSelectedItems: '/contract/sale/batchSelectedItems',
          batchDeletedItems: '/contract/sale/batchDeletedItems',
          importSaleContractDetail: '/contract/sale/importSaleContractDetail',
          findInquiryTemplate: '/inquiry/template/findInquiryTemplate',
          setAnalysisTemplate: '/inquiry/template/setAnalysisTemplate',
          updatePurchaseItems: 'purchase/purchasePlan/updatePurchaseItems',
          exportSaleItems: '/contract/sale/exportSaleItems',
          sendPurchase: '/contract/sale/sendPurchase',
        }
      }
    },
    created() {
      if (this.$route.query){
        this.saleId = this.$route.query.saleId
        this.proId = this.$route.query.proId
        this.toSearch()
      }
    },
    methods:{
      
      changeProject(){
        postActionByJson(this.url.sendPurchase, {proId: this.project.id, ids: this.selectedRowKeysItems})
        .then(resp =>{
          this.$message.success(resp.data + '条发送成功')
        }).catch((e)=>{
          this.$message.error(e)
        }).finally(()=>{
          this.project.visible = false
        })
      },
      async sendPurchase(){
        if(this.selectedRowKeysItems.length > 0){
          await this.loadProject()
          this.project.visible = true
        }
      },
      rowClick(row, index) {
        return {
          on: {
            click: () => {
              let index = this.selectedRowKeysItems.indexOf(row.id)
              if(index === -1){
                this.selectedRowKeysItems.push(row.id)
              }else{
                this.selectedRowKeysItems.splice(index, 1)
              }
            }
          }
        }
      },
      sortBykey,
      download(){
        this.downloadLoading = true
        import('@/vendor/Export2Excel').then(async excel => {
          let tHeader = ['序号', '设备名称', '数量', '需方物资代码', '规格型号', '单位', '交货时间', '使用地点', '工程代码', 
            '联系人','供货单价','供货总额']
          let filterVal = ['sort', 'item', 'number', 'code', 'model', 'unit', 'deliveryTime', 'placeOfUse', 'engineeringCode',
            'contacts', 'salePrice', 'totalPrice']
          let list = []
          const ids = this.selectedRowKeysItems
          if(ids.length != 0){
            await postActionByQueryString(this.url.exportSaleItems, {ids: ids})
            .then(resp =>{
                resp.data.map(i =>{
                let obj = {}
                filterVal.map(k =>{
                  obj[k] = i[k]
                })
                list.push(obj)
              })
            })
          }
          list = this.sortBykey(list, 'sort')
          const data = this.formatJson(filterVal, list)
          excel.export_json_to_excel({
            header: tHeader,
            data,
            filename: '销售合同'
          })
        }).catch((e)=>{
          this.$message.error(e)
        }).finally(()=>{
          this.downloadLoading = false
        })
      },
      formatJson(filterVal, jsonData) {
        return jsonData.map(v => filterVal.map(j => v[j]))
      },
      toSearchItemData(){
        this.loading = true
        this.selectedRowKeysItems = []
        getAction(this.url.findSaleItemBySaleId, {
          saleId : this.saleId,
          pageNum : this.pagination.current,
          pageSize : this.pagination.pageSize
        })
        .then(resp => {
          if (resp.data){
            this.itemData = resp.data.records
            this.pagination.current = parseInt(resp.data.current)
            this.pagination.pageSize = parseInt(resp.data.size)
            this.pagination.total = parseInt(resp.data.total)
            //取出所有的inquiryId
            this.itemData.map(item =>{
              this.itemIds.push(item.id)
            })
          }
        }).catch(()=>{
          this.$message({ type:'warning', message: "查询有误" })
        }).finally(()=>{
          this.loading = false
        })
      },
      updateCommit(){
        postActionByQueryString(this.url.updatePurchaseItems, this.updateDialog.form)
        .then(resp =>{
          this.$message.success(resp.message)
        }).catch(()=>{
          this.$message.error("修改错误")
        }).finally(()=>{
          this.updateDialog.visible = false
          this.toSearchItemData()
          this.selectedRowKeysItems = []
        })
      },
      updateItem(record){
        if(this.selectedRowKeysItems.length > 1){
          this.$message({ type: 'error', message: '只能单个修改，请勿多选' })
        }else {
          this.updateDialog.visible = true
        }
      },
      toFlush(){
        this.toSearch()
      },
      toSearchStock(){

      },
      loadTemplate(){
        getAction(this.url.findInquiryTemplate)
        .then(resp => {
          this.templateDialog.dataSource = resp.data
        }).catch(()=>{
          this.$message({ type :'error', message :'模板加载失败' })
        })
      },
      templateSelected(){
        if (this.itemIds.length > 0) {
          this.loadTemplate();
          this.templateDialog.visible = true;
        }else {
          this.$message({ type :'error', message :'无明细需设置模板' })
        }
      },
      setAnalysisTemplate(){
        this.templateDialog.loading = true
        postActionByQueryString(this.url.setAnalysisTemplate, {proId :this.proId, templateId :this.templateDialog.form.templateId})
        .then(resp =>{
          this.$message({ type :'success', message :resp.message + '条解析模板设置成功' })
        }).catch(()=>{
          this.$message({ type :'error', message :'解析模板设置失败' })
        }).finally(()=>{
          this.templateDialog.visible = false
          this.templateDialog.loading = false
        })
      },
      clickFileInput(){
        this.$refs.upload.dispatchEvent(new MouseEvent('click'))
      },
      batchDeletedItems(){
        if (this.selectedRowKeysItems.length > 0){
          this.$confirm('此操作将永久删除, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(()=>{
            let items = {}
            items["saleId"] = this.saleId
            items["items"] = this.selectedRowKeysItems
            postActionByJson(this.url.batchDeletedItems, items)
            .then(resp => {
              this.$message({ type:'success', message: resp.message })
            }).catch(()=>{
              this.$message({ type:'error', message: '删除失败' })
            }).finally(()=>{
              this.selectedRowKeysItems = []
              this.toSearch()
            })
          }).catch(()=>{
            this.$message({
            type: 'info',
            message: '已取消删除'
          }); 
          })
        }else {
          this.$message({ type:'warning', message: "请选择所需删除项" })
        }
      },
      batchSelected(){
        if (this.selectedRowKeys.length > 0){
          let items = {}
          items["saleId"] = this.saleId
          items["items"] = this.selectedRowKeys
          postActionByJson(this.url.batchSelectedItems, items)
          .then(resp => {
            this.$message({ type:'success', message: resp.message })
            this.toSearch();
          }).catch(()=>{
            this.$message( {type : 'warning', message : '选取失败'} )
          }).finally(()=>{
            this.dialog.submitLoading = false
            this.dialog.visible = false
          })
        }else {
          this.$message({ type:'warning', message: "请选择所需添加项" })
        }
      },
      itemsSelectionChange(ids, rows){
        const itemsId = []
        ids.map(item => {
            itemsId.push(item)
        })
        this.selectedRowKeysItems = itemsId
        if (rows.length > 0){
          this.updateDialog.form = rows[0]
        }
      },
      indexHandleSelectionChange(ids, rows) {
        const selectedId = []
        ids.map(item => {
          selectedId.push(item)
        })
        this.selectedRowKeys = selectedId
      },
      toSearchItems(){
        this.dialog.loading = true
        postActionByQueryString(this.url.findItemsAndSupplyByProjectId, {
          proIds: this.dialog.form.proDetailIds,
          name: this.dialog.form.name,
          type: 'sale',
          pageNum: this.dialog.pagination.current,
          pageSize: this.dialog.pagination.pageSize
        })
        .then(resp => {
          if (resp.data){
            this.dialogData = resp.data.records
            this.dialog.pagination.current = parseInt(resp.data.current)
            this.dialog.pagination.pageSize = parseInt(resp.data.size)
            this.dialog.pagination.total = parseInt(resp.data.total)
          }
        }).catch(()=>{
          this.$message({ type:'warning', message: "查询有误" })
        }).finally(()=>{
          this.dialog.loading = false
        })
      },
      async handleAdd(){
        //初始化数据
        this.selectedRowKeys = []
        this.projects = []
        this.searchForm = {}
        this.dialogData = []
        await this.loadProject()
        this.dialog.visible = true
      },
      loadProject(){
        getAction(this.url.findAllProject)
        .then(resp => {
          if (resp.data){
            this.projects = resp.data
          }
        }).catch(()=>{
          this.$message.error('项目加载失败')
        }).finally(()=>{
          
        })
      },
      dateTimeFormat,
      toSearch(){
        this.loading = true
        getAction(this.url.findSaleContractDetail, {saleId : this.saleId})
        .then(resp => {
          if (resp.data){
            this.item = resp.data
            this.title = this.item.contractNo
          }
        }).catch(()=>{
          this.$message({ type:'warning', message: "查询有误" })
        })
        this.toSearchItemData()
      },
      readExcel(e) {
        if (this.saleId) {
          const files = e.target.files;
          if (files.length <= 0) {//如果没有文件名
            return false;
          } else if (!/\.(xls|xlsx)$/.test(files[0].name.toLowerCase())) {
            this.$message.error('上传格式不正确，请上传xls或者xlsx格式');
            return false;
          }
          this.excelRows = 0
          const fileReader = new FileReader();

          fileReader.onload = (ev) => {
            try {
              const data = ev.target.result;
              const workbook = XLSX.read(data, {
                type: 'binary'
              });
              const wsname = workbook.SheetNames[0];//取第一张表
              const ws = XLSX.utils.sheet_to_json(workbook.Sheets[wsname]);//生成json表格内容
              console.log(ws)
              this.outputs = [];//清空接收数据
              ws.map(item => {
                let excelData = {}
                console.info(item)
                excelData['code'] = item["需方物资代码"]
                excelData['sort'] = item["序号"]
                excelData['item'] = item["设备名称"]
                excelData['model'] = item["规格型号"]
                excelData['unit'] = item["单位"]
                excelData['number'] = item["数量"]
                excelData['salePrice'] = item["供货单价"]
                excelData['deliveryTime'] = item["交货时间"]
                excelData['placeOfUse'] = item["使用地点"]
                excelData['engineeringCode'] = item["工程代码"]
                excelData['contacts'] = item["联系人"]

                excelData['saleContractId'] = this.saleId
                excelData['projectId'] = this.item.projectId
                this.outputs.push(excelData)
                this.excelRows++
              })
              this.$refs.upload.value = '';
            } catch (e) {
              return false;
            }

            if (this.outputs){
              let map = {};
              map['detail'] = this.outputs
              postActionByJson(this.url.importSaleContractDetail, map)
              .then(resp =>{
                this.$message({ type : 'success', message : resp.message + '导入成功'})
              }).catch(()=>{
                this.$message({ type : 'error', message : '导入失败'})
              }).finally(()=>{
                this.toSearch(this.saleId)
              })
            }else {
              this.$message({type : 'error', message : '无数据可导入'})
            }
          };
          fileReader.readAsBinaryString(files[0]);
        }else {
          this.$message({ type : 'error', message : '无销售合同ID'})
        }
      },
    }
  }

</script>

<style lang="scss" scoped>
.contract-sale-detail {
  /deep/.el-form-item__content{
    height:auto;
    line-height:32px;
    margin-left:90px!important
  }
  .outputlist_upload {
    opacity: 0;
    width: 0;
    overflow: hidden;
  }
}
</style>
